//
//  MMMenuView.m
//  MemeMaker
//
//  Created by Phan Quang Ha on 7/16/13.
//  Copyright (c) 2013 soyo. All rights reserved.
//

#import "MMMenuView.h"

@interface MMMenuView ()
{
    MY_POSITION myPos;
    CGRect superRect;
    
    UIButton *btnMain;
    UIButton *btnAction1;
    UIButton *btnAction2;
    UIButton *btnAction3;
    
    BOOL firstLoad;
}

@end

@implementation MMMenuView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        btnMain = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
        [btnMain addTarget:self action:@selector(didTouchMenuButton) forControlEvents:UIControlEventTouchUpInside];
        
        btnAction1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
        btnAction2 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
        btnAction3 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
        
        [self addSubview:btnAction3];
        [self addSubview:btnAction2];
        [self addSubview:btnAction1];
        [self addSubview:btnMain];
    }
    return self;
}
- (id)initWithPosition:(MY_POSITION)pos inFrame:(CGRect)frame
{
    CGRect mframe;
    mframe.size.width = 44;
    mframe.size.height = 44;
    
    switch (pos) {
        case kTOP_RIGHT:
        {
            mframe.origin.x = frame.size.width - 44;
            mframe.origin.y = 0;
        }
            break;
        case kBOT_LEFT:
        {
            mframe.origin.y = frame.size.height - 44;
            mframe.origin.x = 0;
        }
            break;
        case kBOT_RIGHT:
        {
            mframe.origin.x = frame.size.width - 44;
            mframe.origin.y = frame.size.height - 44;
        }
            break;
        default:
            break;
    }
    
    self = [self initWithFrame:mframe];
    if (self) {
        myPos = pos;
        superRect = frame;
    }
    return self;
}

- (void)setMainImage:(UIImage *)img
{
    [btnMain setBackgroundImage:img forState:UIControlStateNormal];
}
- (void)set1stAction:(SEL)action target:(id)target andImage:(UIImage *)img
{
    [btnAction1 setBackgroundImage:img forState:UIControlStateNormal];
    [btnAction1 addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [btnAction1 addTarget:self action:@selector(hideButtons) forControlEvents:UIControlEventTouchUpInside];
}
- (void)set2ndAction:(SEL)action target:(id)target andImage:(UIImage *)img
{
    [btnAction2 setBackgroundImage:img forState:UIControlStateNormal];
    [btnAction2 addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [btnAction2 addTarget:self action:@selector(hideButtons) forControlEvents:UIControlEventTouchUpInside];
}
- (void)set3rdAction:(SEL)action target:(id)target andImage:(UIImage *)img
{
    [btnAction3 setBackgroundImage:img forState:UIControlStateNormal];
    [btnAction3 addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [btnAction3 addTarget:self action:@selector(hideButtons) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - Show Buttons;
- (void) showButtons
{
    [UIView animateWithDuration:0.5 animations:^{
        CGRect frame = self.frame;
        frame.size.width = 90;
        frame.size.height = 90;
        
        CGRect frame1 = btnAction1.frame;
        CGRect frame2 = btnAction2.frame;
        CGRect frame3 = btnAction3.frame;
        CGRect frame4 = btnMain.frame;
        
        switch (myPos) {
            case kTOP_LEFT:
            {
                frame1.origin.x = 45;
                frame1.origin.y = 0;
                
                frame2.origin.x = 45;
                frame2.origin.y = 45;
                
                frame3.origin.x = 0;
                frame3.origin.y = 45;
                
                frame4.origin.x = 0;
                frame4.origin.y = 0;
            }
                break;
            case kTOP_RIGHT:
            {
                frame.origin.x = superRect.size.width - 90;
                
                frame1.origin.y = 0;
                frame1.origin.x = 0;
                
                frame2.origin.x = 0;
                frame2.origin.y = 45;
                
                frame3.origin.y = 45;
                frame3.origin.x = 45;
                
                frame4.origin.x = 45;
                frame4.origin.y = 0;
            }
                break;
            case kBOT_LEFT:
            {
                frame.origin.y = superRect.size.height - 90;
                
                frame1.origin.y = 45;
                frame1.origin.x = 45;
                
                frame2.origin.x = 45;
                frame2.origin.y = 0;
                
                frame3.origin.y = 0;
                frame3.origin.x = 0;
                
                frame4.origin.x = 0;
                frame4.origin.y = 45;
            }
                break;
            case kBOT_RIGHT:
            {
                frame.origin.x = superRect.size.width - 90;
                frame.origin.y = superRect.size.height - 90;
                
                frame1.origin.y = 45;
                frame1.origin.x = 0;
                
                frame2.origin.x = 0;
                frame2.origin.y = 0;
                
                frame3.origin.y = 0;
                frame3.origin.x = 45;
                
                frame4.origin.x = 45;
                frame4.origin.y = 45;
            }
                break;
            default:
                break;
        }
        self.frame = frame;
        
        btnAction1.frame = frame1;
        btnAction2.frame = frame2;
        btnAction3.frame = frame3;
        btnMain.frame = frame4;
    } completion:^(BOOL finished) {
        self.isShowing = YES;
    }];
}
- (void) hideButtons
{
    [UIView animateWithDuration:0.5 animations:^{
        CGRect mframe = self.frame;
        
        switch (myPos) {
            case kTOP_RIGHT:
            {
                mframe.origin.x = superRect.size.width - 44;
            }
                break;
            case kBOT_LEFT:
            {
                mframe.origin.y = superRect.size.height - 44;
            }
                break;
            case kBOT_RIGHT:
            {
                mframe.origin.x = superRect.size.width - 44;
                mframe.origin.y = superRect.size.height - 44;
            }
                break;
            default:
                break;
        }
        mframe.size.width = 44;
        mframe.size.height = 44;
        self.frame = mframe;
        
        btnAction1.frame = btnAction2.frame = btnAction3.frame = btnMain.frame = CGRectMake(0, 0, 44, 44);
    } completion:^(BOOL finished) {
        self.isShowing = NO;
    }];
}

- (void) didTouchMenuButton
{
    if (self.isShowing) {
        [self hideButtons];
    }else{
        [self showButtons];
    }
}
- (void)bringMainActionToFront
{
    [self bringSubviewToFront:btnMain];
}
- (void)bring1stActionToFront
{
    [self bringSubviewToFront:btnAction1];
}
- (void)bring2ndActionToFront
{
    [self bringSubviewToFront:btnAction2];
}
- (void)bring3rdActionToFront
{
    [self bringSubviewToFront:btnAction3];
}


@end
